#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

const char dir[50] = "/home/allen/notes/";
const char smile[10] = "~^_^~";
const char my_name[10] = "Lin Yi";

void print_smile(){
	printf("\n%s\n", smile);	
}

int main()
{
	char input[256];
	
	time_t raw_time;
	struct tm *time_info;
	char *asc_time2;
	char asc_time[255];

	time(&raw_time);
	time_info = localtime(&raw_time);
	asc_time2 = asctime(time_info);
	strcpy(asc_time, &asc_time2[8]);
	strcpy(&asc_time[3], &asc_time2[4]);
	strcpy(&asc_time[6], &asc_time2[19]);
	

	int i = 0;
	for(i = 0; i < 11; i ++){	
		if(asc_time[i] == ' ' || asc_time[i] == ':'){
			asc_time[i] = '_';
		} else if (asc_time[i] == '\n') {
			asc_time[i] = '\0';
		}
	}
	asc_time[11] = '\0';
	
	char path[256];
	strcpy(path, dir);
	strcat(path, asc_time);
	FILE *note = fopen(path, "a");

	char todo_path[256];
	strcpy(todo_path, dir);
	strcat(todo_path, "todos");
	
	if(note == NULL) {
		printf("error creating the note. "
			"I'm trying to save it here: %s\n", path);
		exit(0);
	}

	print_smile();
	printf("Hi, Dear Lin, Yi:)\n");	
	printf("I'm saving this note in %s\n", path);
	fprintf (note, "-----------------------------------------\n");
	fprintf(note, "|    %s", asc_time2);
	fprintf(note, "-----------------------------------------\n|\n");
	printf("And saving it with title %s\n", asc_time2);

	
	while(1){
		printf("> ");		
		fgets(input, sizeof(input), stdin);
		
		if(input[1] == '\n') {
			if(input[0] == 'e') {
				print_smile();
				printf("This note is saved in %s\n", path);
				
				fprintf(note, "|\n|    %s", asc_time2);
				fprintf(note, "-----------------------------------------\n\n");
				printf("I'm logging you out... done. See you later :)\n\n");
				break;
			}

			if(input[0] == 't') {
				printf("\nYes, I'm listening to you right now:)\n\n");
				continue;
			} 

			if(input[0] == 's'){
				
				print_smile();
				printf("Sure, I could do that for you~\n");
				printf("This note is saved in %s\n\n", path);
			}
	
			if(input[0] == 'v') {
				printf(" Opening the VIM for you...\n\n");
				continue;	
			} 
		}

		if(input[2] == '\n'){
			if(strcmp(input, "hi\n") == 0) {
				print_smile();
				printf("Hi, %s, how are you? Have a good day!\n", my_name);
				continue;
			}

			if(strcmp(input, "td\n") == 0){
				print_smile();
				printf("Here is your todo list~");
				
				FILE *todo_file = fopen(todo_path, "r");
				if(todo_file == NULL){
					/*message for error*/
					continue;
				}
			
				
				continue;
			}
		}

		if(input[0] == 't' && input[1] == 'd'){
			/*this is a todo keyword start. Adding line to the todo list as well*/
			FILE *todo_file = fopen(todo_path, "a");
			if(todo_file == NULL){
				printf("Sorry, something wrong with the todo file > <%s\n", todo_path);
				continue;
			}	
			fprintf(todo_file, "      todo @ %s", asc_time2);
			fprintf(todo_file, "%s\n", input);
			fclose(todo_file);

			printf("I've sent it to your todo list, with start time %s", asc_time2);
		}

		if(input[0] == 't' && input[1] == 'a' && input[2] == 'g'){
			if(input[3] == '\n'){
				/*showing the tags I've been using*/
				continue;
			}

			input[strlen(input)-1] = '\0';
			/*this is a tag keyword start. Switch to a note with the start of that tag*/
			print_smile();	
			printf("I'm switching you to \"%s\"\n", &input[4]);
			fclose(note);
			strcpy(path, dir);
			strcat(path, &input[4]);
			strcat(path, "_");
			strcat(path, asc_time);
	
			note = fopen(path, "a");
			if(note == NULL){
				printf("Sorry, I failed opening the file %s for writing\n", path);
			}
			
		}
		printf("\n");
		fprintf(note, "| %s", input);
	}

	fclose(note);
	exit(0);
}
